//Publicly-available code, obtained from: 
//https://aptframework.dev.java.net/jelly/passwordHash.html
package Password;
import java.security.*;
import sun.misc.BASE64Encoder;

public class PasswordHasher 
{
    String algorithm = "SHA-1";
    public PasswordHasher() 
	{
    }
    
    public PasswordHasher(String algorithm) 
	{
        this.algorithm = algorithm;
    }
    
    public String hashPassword(byte[] passwordBytes) 
			throws NoSuchAlgorithmException 
	{
		BASE64Encoder b64 = new BASE64Encoder();
        MessageDigest digest = MessageDigest.getInstance(algorithm);
        byte[] hashBytes = digest.digest(passwordBytes);
        String hashString = ""+ b64.encode(hashBytes);
        return hashString;
    }
    public boolean verifyPassword(byte[] passwordBytes, String passwordHash) 
	    throws NoSuchAlgorithmException 
	{
	        return hashPassword(passwordBytes).equals(passwordHash);
 	}
	/*public static void main(String[] args) 
	{
		try 
		{
      		String password = "password";
      		PasswordHasher passwordHasher = new PasswordHasher("SHA-256");
      		byte[] passwordBytes = password.getBytes();
      		String passwordHash = passwordHasher.hashPassword(passwordBytes);
      		System.out.println(passwordHash.length());
      		System.out.println(passwordHash);
      		System.out.println(passwordHasher.hashPassword(password.getBytes()));
      		byte[] wrongPasswordBytes = password.toUpperCase().getBytes();
      		String wrongHash = passwordHasher.hashPassword(wrongPasswordBytes);
      		System.out.println(wrongHash);            
      		System.out.println(
              passwordHasher.verifyPassword(passwordBytes, passwordHash));
      		System.out.println(
              passwordHasher.verifyPassword(wrongPasswordBytes, passwordHash));
  		} 
		catch (Exception e) 
		{
      			e.printStackTrace();
	    }
	}*/

}